<?php
defined("_VALID_ACCESS") || die('Direct access forbidden');
class SCTV_InformationRequestCommon extends ModuleCommon {
    public static function menu() {
    	$menus = array('__submenu__' => 2);
		$menus[__('Request')] = array(); 
		$menus[__('Information Group')] = array('__function__'=>'information_group');
		$menus[__('Information')] = array('__function__'=>'information');
		
		$manager =  CRM_ContactsCommon::get_my_record();
	
		if(Base_AclCommon::i_am_admin() || array_key_exists('area manager', $manager['access']))
			$menus[__('Print request')]= array('__function__'=>'print_request');
					
        return array(
        	__('SCTV') => array
        	(
        		'__submenu__' => 1, 
        		__('Information Request') =>$menus        		
			));
    }
	public static function display_information($record, $nolink, $desc) {
		$information = $record['information'];
		$record = Utils_RecordBrowserCommon::get_record('SCTV_Information', $information);
		return $record['name'];
	}
	public static function QFfield_information(&$form, $field, $label, $mode, $default, $desc, $rb_obj){
		if ($mode=='add' || $mode=='edit')
		{
			$form->addElement('select', $field, $label, array(), array('id'=>$field));
			
            $form->setDefaults(array($field=>$default));
			
			Utils_ChainedSelectCommon::create($field, array('information_group'),'modules/SCTV/InformationRequest/update_information.php',null,$form->exportValue($field));			
		}		
		else
		{
			$record = Utils_RecordBrowserCommon::get_record('SCTV_Information', $default);
        	$form->addElement('static', $field, $label, $record['name']);
        }
    }
	public static function display_status($record, $nolink, $desc) {
		$rb_obj = Utils_RecordBrowser::$rb_obj; 
		return self::display_status_object($rb_obj, $record, $nolink, $desc);		
	}
	public static function display_status_object($object, $record, $nolink, $desc) {
		$status = $record['status'];
		$request_status = Utils_CommonDataCommon::get_translated_array('SCTV/RequestStatus');
		if($object == null) return $request_status[$status];
		
		if($status < 3)
		{
			$select_status = $object->init_module('Utils_LeightboxPrompt');
			$form = $object->init_module('Libs_QuickForm');
        	$form->addElement('text', 'note', __('Note'));
        	$form->addRule('note', 'Field required', 'Required');
							
			$manager =  CRM_ContactsCommon::get_my_record();	
		
		
			if($status <= 0 && array_key_exists('area manager', $manager['access']))
			{
				$select_status->add_option('resolved', __('Resolved'), null);
				$select_status->add_option('reject', __('Reject'), null, $form);					
			}
			else
			{
				$select_status->add_option('open', __('Reopen'), null, $form);
				$select_status->add_option('close', __('Close'), null);				
			}								
			
			$object->display_module($select_status, array(__( 'Change status'), array('requirement')));
			$vals = $select_status->export_values();
			if (isset($vals['params']['requirement']) && $vals['params']['requirement'] == $record['id'] && !isset($_REQUEST['__location']))
			{
				if ($vals['option'] == 'open'){
                	$status = '0';                	
                	Utils_RecordBrowserCommon::update_record('SCTV_InformationRequest', $record['id'], array('status' => $status));
					
					$note = __('Open') . ': ' . $vals['form']['note'];
					Utils_AttachmentCommon::add('SCTV_InformationRequest/'.$record['id'],0,Acl::get_user(),$note);
            		location(array());
    	        }
				elseif ($vals['option'] == 'resolved'){
                	$status = '1';
                	Utils_RecordBrowserCommon::update_record('SCTV_InformationRequest', $record['id'], array('status' => $status));
					
					$note = __('Resolved');
					Utils_AttachmentCommon::add('SCTV_InformationRequest/'.$record['id'],0,Acl::get_user(),$note);
            		location(array());
    	        }
				elseif ($vals['option'] == 'reject'){
                	$status = '2';
                	Utils_RecordBrowserCommon::update_record('SCTV_InformationRequest', $record['id'], array('status' => $status));
					
					$note = __('Closed') . ': ' . $vals['form']['note'];
					Utils_AttachmentCommon::add('SCTV_InformationRequest/'.$record['id'],0,Acl::get_user(),$note);
            		location(array());
    	        }
				elseif ($vals['option'] == 'close'){
                	$status = '3';                	
                	Utils_RecordBrowserCommon::update_record('SCTV_InformationRequest', $record['id'], array('status' => $status));
					
					$note = __('Close');
					Utils_AttachmentCommon::add('SCTV_InformationRequest/'.$record['id'],0,Acl::get_user(),$note);
					
            		location(array());
    	        }		            	
	        } 

			$href = $select_status->get_href(array('requirement' => $record['id']));
			return '<a ' . $href . '>' . $request_status[$status] . '</a>';	
		}
		else
			return $request_status[$status];
	}
 	
	public static function QFfield_status(&$form, $field, $label, $mode, $default, $desc, $rb_obj){
		if($mode=='add')
		{
			$data = Utils_CommonDataCommon::get_translated_array('SCTV/RequestStatus');
			$form->addElement('select', $field, $label, array(0=>$data[0]), array('id'=>$field)); 
        	$form->setDefaults(array($field=>$default));
		}
		elseif($mode=='edit')
		{
			$form->addElement('commondata', $field, $label, 'SCTV/RequestStatus', array('empty_option'=>false), array('id'=>$field)); 
        	$form->setDefaults(array($field=>$default));
		}
		else
		{
			$val = self::display_status($rb_obj->record, false, $desc); 
    		$form->addElement('static', $field, $label, $val);
     	}	
    }
}

?>